package com.browan.background;

import java.net.URL;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Wallpaper extends Activity
{

	private String url;

	private WebView webView;

	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		setTitle("壁纸预览");

		setContentView(R.layout.wallpaper);

		url = getIntent().getStringExtra(Background.URL);

		webView = (WebView) findViewById(R.id.web);

		progressBar = (ProgressBar) findViewById(R.id.progressbar);

		final Activity activity = this;

		webView.setWebChromeClient(new WebChromeClient()
		{

			public void onProgressChanged(WebView view, int progress)
			{
				activity.setProgress(progress * 100);

				if (100 == progress)
				{
					progressBar.setVisibility(View.GONE);
				}
			}
		});

		isClick = true;

		findViewById(R.id.setwalldaper).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (isClick)
				{
					try
					{

						isClick = false;

						new Thread(new Runnable()
						{

							@Override
							public void run()
							{

								message = "设置壁纸成功！";

								try
								{
									setWallpaper(BitmapFactory.decodeStream(new URL(url)
											.openStream()));
								}
								catch (Exception e)
								{
									message = "设置壁纸失败！";
									e.printStackTrace();
								}

								webView.post(new Runnable()
								{

									@Override
									public void run()
									{
										Toast.makeText(Wallpaper.this, message, Toast.LENGTH_SHORT)
												.show();
									}
								});

							}
						}).start();

					}
					catch (Exception e)
					{
						e.printStackTrace();
					}

				}

			}
		});
		webView.setWebViewClient(client);

		findViewById(R.id.big).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				webView.zoomIn();

			}
		});

		findViewById(R.id.litter).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				webView.zoomOut();
			}
		});

		webView.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{

				boolean ret = false;

				switch (event.getAction())
				{
				case MotionEvent.ACTION_DOWN:
					lastTime = 0;
					isFirst = true;
					break;
				default:
					break;
				}

				if (2 == event.getPointerCount())
				{
					doWithMutiPoint(event);
					ret = true;
				}

				return ret;
			}
		});

		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				webView.loadUrl(url);
			}
		}, 1500);

	}

	Handler handler = new Handler();

	private boolean isClick;

	private String message;

	WebViewClient client = new WebViewClient()
	{
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon)
		{
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url)
		{
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url)
		{
			super.onPageFinished(view, url);
			progressBar.setVisibility(View.GONE);
			isClick = true;
		}

		@Override
		public void onReceivedError(WebView view, int errorCode, String description,
				String failingUrl)
		{
			// super.onReceivedError(view, errorCode, description, failingUrl);
			view.loadDataWithBaseURL("about:blank", "载入图片出错！", "text/html", "UTF-8", null);
			progressBar.setVisibility(View.GONE);
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
	}

	private double lastDistence;
	private double currentDistence;

	// private String TAG = getClass().getSimpleName();

	private long lastTime;

	private void doWithMutiPoint(MotionEvent event)
	{

		long currentTime = System.currentTimeMillis();

		if (currentTime - lastTime > 100)
		{
			lastTime = currentTime;
		}
		else
		{
			return;
		}

		int id1 = event.getPointerId(0);

		int id2 = event.getPointerId(1);

		float x1 = event.getX(id1);
		float y1 = event.getY(id1);

		float x2 = event.getX(id2);
		float y2 = event.getY(id2);

		switch (event.getAction())
		{
		case MotionEvent.ACTION_MOVE:

			if (isFirst)
			{
				isFirst = false;
				currentDistence = lastDistence = Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1)
						* (x2 - x2));
			}
			else
			{
				currentDistence = Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x2));
				//
				// if (currentDistence > lastDistence)
				// {
				// webView.zoomIn();
				// }
				// else if (currentDistence < lastDistence)
				// {
				// webView.zoomOut();
				// }
				// else
				// {
				// // TODO
				// }

				if (currentDistence > lastDistence
						&& Math.abs(currentDistence - lastDistence) > 120)
				{
					// 放大
					// Log.e(TAG, "Big");
					webView.zoomIn();
					lastDistence = currentDistence;

				}
				else if (currentDistence < lastDistence
						&& Math.abs(currentDistence - lastDistence) < 120)
				{// 缩小
				// Log.e(TAG, "Litter");
					webView.zoomOut();
					lastDistence = currentDistence;
				}
				else
				{
					// TODO
				}
			}
			break;
		default:
			break;
		}
	}

	private boolean isFirst;

}